package com.tarena.ELSFK.day03_all.tetris;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 * 俄罗斯方块游戏面板 
 *
 */
public class Tetris extends JPanel {
	/** 正在下落方块 */
	private Tetromino tetromino;
	/** 下一个下落方块 */
	private Tetromino nextOne;
	/** 墙 */
	private Cell[][] wall;
	
	public void action(){
		tetromino = Tetromino.randomTetromino();
		nextOne = Tetromino.randomTetromino();
		repaint();
		KeyAdapter l = new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int key = e.getKeyCode();
				//System.out.println(key); 
				switch(key){
				case KeyEvent.VK_RIGHT: tetromino.moveRight(); break;
				case KeyEvent.VK_LEFT: tetromino.moveLeft(); break;
				case KeyEvent.VK_DOWN: tetromino.softDrop(); break;
				}
				repaint();
			}
		};
		this.requestFocus();
		this.addKeyListener(l);
	}
	
	public void paint(Graphics g){
		g.setColor(new Color(0xdddddd));
		g.fillRect(0, 0, this.getWidth(), this.getHeight()); 
		Cell[] cells = tetromino.getCells();
		for(int i=0; i<cells.length; i++){
			Cell c = cells[i];
			int x = c.getCol() * 25;
			int y = c.getRow() * 25;
			g.setColor(new Color(c.getColor()));
			g.fillRect(x, y, 25, 25);
		}
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		Tetris tetris = new Tetris();
		frame.add(tetris);
		frame.setSize(500, 550);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//Location 位置 RelativeTo相对于 
		frame.setLocationRelativeTo(null);//使当前窗口居中
		frame.setVisible(true);
		tetris.action();
		//tetris.requestFocus();
	}
}







